package com.eece496.Server;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eece496.Database.Attendance;
import com.eece496.Database.COGSAssignments;
import com.eece496.Database.Grades;
import com.eece496.Database.StudentGroups;
import com.eece496.Database.TASaveData;
import com.eece496.Database.TAduties;
import com.eece496.Helpers.TADutiesInfo;

/**
 * Servlet implementation class TACommitGradesServlet
 */
public class TACommitGradesServlet extends MasterServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TACommitGradesServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Connect();
		String division = request.getParameter("division");
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String courseNumber = request.getParameter("courseNumber");
		String sectionNumber = request.getParameter("sectionNumber");
		String tID = request.getParameter("TAID");
		String button = request.getParameter("button");
		int TAID = Integer.parseInt(tID);
		String empty = "empty";
		System.out.println(tID);
		String COGSName = request.getParameter("COGSName");
		TAduties TADutiesDB = new TAduties();
		TADutiesDB.Setup(connection);

		String make = request.getParameter("viewButton");
		System.out.println("jkldhafkjlsdhkfahdshfds" +make);
		String[] valueArray = request.getParameterValues("Attend");
		// System.out.println(valueArray.length);

		//System.out.pr
		if (make.equals("Make Assessment")) {
			for (int i = 0; i < valueArray.length; i++) {
				String sID = valueArray[i].substring(0, 8);
				// System.out.println(sID);
				Integer studentID = Integer.parseInt(sID);

				String status = valueArray[i].substring(8);
				// System.out.println(status);

				Attendance AttendanceDB = new Attendance(studentID, status,
						COGSName, courseNumber, sectionNumber);
				AttendanceDB.Setup(connection);
				try {
					AttendanceDB.Insert();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
		// System.out.println

		ArrayList<TADutiesInfo> info = new ArrayList<TADutiesInfo>();

		ArrayList<String> IndGrades = new ArrayList<String>();
		ArrayList<String> GroupGrades = new ArrayList<String>();
		ArrayList<String> AssignmentIDs = new ArrayList<String>();
		ArrayList<Integer> StudentIDS = new ArrayList<Integer>();
		ArrayList<Integer> groupNumbers = new ArrayList<Integer>();
		ArrayList<Integer> studentList = new ArrayList<Integer>();

		String buttons = request.getParameter("button");

		System.out.println("The button Value: " + buttons);

		if (buttons.equals("Finish Assessment")) {

			System.out.println("WHAT UP DOG????");
			// Get Indivdual Grades
			for (int i = 0; i < 6; i++) {
				String str = request.getParameter("IndGrade" + (i + 1));
				System.out.println(str);
				IndGrades.add(str);
			}

			// Get Group Grades
			for (int i = 0; i < 6; i++) {
				String str = request.getParameter("GroupGrade" + (i + 1));
				System.out.println(str);
				GroupGrades.add(str);
			}

			// Get COGS Assignments
			COGSAssignments COGSAssignmentDB = new COGSAssignments();
			COGSAssignmentDB.Setup(connection);
			try {
				AssignmentIDs = COGSAssignmentDB.GetAssignmentNames(COGSName,
						courseNumber, sectionNumber);

				// Get StudentIDs from TA duties
				TAduties TAdutiesDB = new TAduties();
				TAdutiesDB.Setup(connection);
				System.out.println(tID);
				System.out.println(COGSName);
				System.out.println(courseNumber);
				System.out.println(sectionNumber);
				StudentIDS = TAdutiesDB.GetStudentIDs(TAID, COGSName,
						courseNumber, sectionNumber, division);

				// Submit Individual Grades
				for (int i = 0; i < 6; i++) {
					Grades GradesDB = new Grades();
					GradesDB.Setup(connection);
					GradesDB.ModifyGrade(IndGrades.get(i), courseNumber,
							sectionNumber, StudentIDS.get(i),
							AssignmentIDs.get(0));

					// Have to change the priority of the students by one if not
					// AB
					if (!IndGrades.get(i).equals("AB"))
						;
					{
						StudentGroups sGroups = new StudentGroups();
						sGroups.Setup(connection);
						int priority = sGroups.GetPriority(StudentIDS.get(i),
								courseNumber, sectionNumber);
						priority = priority + 1;
						sGroups.ModifyPriority(priority, StudentIDS.get(i),
								courseNumber, sectionNumber);
					}
				}

				// Submit Group Grades
				StudentGroups sGroups = new StudentGroups();
				sGroups.Setup(connection);

				for (Integer num : StudentIDS) {
					// Get the group numbers from each student;
					int gNum = sGroups.GetGroupNumberFromStudent(num,
							courseNumber, sectionNumber);
					int gNumNext = 0;

					// check if group Number is odd or even
					// even
					if (gNum % 2 == 0) {
						gNumNext = gNum - 1;
					}

					// odd
					else {
						gNumNext = gNum + 1;
					}

					groupNumbers.add(gNum);
					groupNumbers.add(gNumNext);
					for (int i = 0; i < groupNumbers.size(); i++)
						System.out.println(groupNumbers.get(i));
				}
				int counter = 1;
				int count = 1;
				int gradecount = 0;
				for (Integer num : groupNumbers) {
					Attendance AttendanceDB = new Attendance();
					AttendanceDB.Setup(connection);
					studentList = sGroups.GetStudentsFromGroup(num,
							courseNumber, sectionNumber, division);
					for (Integer num1 : studentList) {
						Grades GroupGradesDB = new Grades();
						GroupGradesDB.Setup(connection);
						String str = null;
						String absent = "AB";
						str = AttendanceDB.getStatus(num1, COGSName,
								courseNumber, sectionNumber);
						// If the student is absent
						if (str.equals("A"))
							GroupGradesDB.ModifyGrade(absent, courseNumber,
									sectionNumber, num1,
									AssignmentIDs.get(count));
						else {
							GroupGradesDB.ModifyGrade(
									GroupGrades.get(gradecount), courseNumber,
									sectionNumber, num1,
									AssignmentIDs.get(count));
						}
					}
					if (counter % 2 == 0) {
						gradecount++;
						count++;
					}
					counter++;
				}

				// Set the TADuties to complete
				TAdutiesDB.ModifyStatus("complete", courseNumber,
						sectionNumber, TAID, division, COGSName);

				request.setAttribute("courseNumber", courseNumber);
				request.setAttribute("sectionNumber", sectionNumber);
				request.setAttribute("TAID", tID);
				request.setAttribute("COGSName", COGSName);
				request.setAttribute("firstname", firstname);
				request.setAttribute("lastname", lastname);

				request.getRequestDispatcher("/TA/TA-Course-Portal.jsp")
						.forward(request, response);

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		// TRASH THIS BROKEN PIECE OF SHIT FOR NOW!!!!
		/*
		 * 
		 * if(buttons.equals("Save Assessment")){ // Get Indivdual Grades for
		 * (int i = 0; i < 6; i++) { String str =
		 * request.getParameter("IndGrade" + (i + 1)); System.out.println(str);
		 * Integer myInt = Integer.parseInt(str); IndGrades.add(myInt); }
		 * 
		 * // Get Group Grades for (int i = 0; i < 6; i++) { String str =
		 * request.getParameter("GroupGrade" + (i + 1));
		 * System.out.println(str); Integer myInt = Integer.parseInt(str);
		 * GroupGrades.add(myInt); }
		 * 
		 * //Save the Individual Grades for( Integer i: IndGrades) { TASaveData
		 * save = new TASaveData(TAID,COGSName,i); save.Setup(connection); try {
		 * save.Insert(); } catch (SQLException e) { // TODO Auto-generated
		 * catch block e.printStackTrace(); } }
		 * 
		 * //Save the Grade Grades for( Integer i: GroupGrades) { TASaveData
		 * save = new TASaveData(TAID,COGSName,i); save.Setup(connection); try {
		 * save.Insert(); } catch (SQLException e) { // TODO Auto-generated
		 * catch block e.printStackTrace(); } System.out.println("HI!"); }
		 * 
		 * try { info = TADutiesDB.GetTADuties(TAID, courseNumber,
		 * sectionNumber); request.setAttribute("TADutyInfo", info);
		 * request.setAttribute("firstname", firstname);
		 * request.setAttribute("lastname", lastname);
		 * request.setAttribute("courseNumber", courseNumber);
		 * request.setAttribute("sectionNumber", sectionNumber);
		 * request.setAttribute("TAID", tID); request.setAttribute("empty",
		 * empty); request.getRequestDispatcher("/TA/TA-View-Assessments.jsp")
		 * .forward(request, response);
		 * 
		 * 
		 * } catch (SQLException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); }
		 * 
		 * 
		 * 
		 * /* TAduties TADutiesDB = new TAduties();
		 * TADutiesDB.Setup(connection);
		 * 
		 * //Save the Individual Grades for( Integer i: IndGrades) { TASaveData
		 * save = new TASaveData(tID,COGSName,i); save.Setup(connection); try {
		 * save.Insert(); } catch (SQLException e) { // TODO Auto-generated
		 * catch block e.printStackTrace(); } }
		 * 
		 * //Save the Grade Grades for( Integer i: GroupGrades) { TASaveData
		 * save = new TASaveData(tID,COGSName,i); save.Setup(connection); try {
		 * save.Insert(); } catch (SQLException e) { // TODO Auto-generated
		 * catch block e.printStackTrace(); } System.out.println("HI!"); }
		 * 
		 * empty ="not empty"; ArrayList<TADutiesInfo> info = new
		 * ArrayList<TADutiesInfo>(); try { info = TADutiesDB.GetTADuties(tID,
		 * courseNumber, sectionNumber); System.out.println("SIZE: " +
		 * info.size()); System.out.println("TA: " + tID);
		 * System.out.println("Course "+ courseNumber);
		 * System.out.println("Section " + sectionNumber); for(TADutiesInfo i:
		 * info) { System.out.println(i.groupnumber); }
		 * //System.out.println(info.get(0).groupnumber);
		 * request.setAttribute("empty", empty);
		 * request.setAttribute("IndGrades",IndGrades );
		 * request.setAttribute("GroupGrades",GroupGrades );
		 * request.setAttribute("TADutyInfo", info);
		 * request.setAttribute("firstname", firstname);
		 * request.setAttribute("lastname", lastname);
		 * request.setAttribute("courseNumber", courseNumber);
		 * request.setAttribute("sectionNumber", sectionNumber);
		 * request.setAttribute("TAID", tID);
		 * request.getRequestDispatcher("/TA/TA-View-Assessments.jsp")
		 * .forward(request, response); } catch (SQLException e) { // TODO
		 * Auto-generated catch block e.printStackTrace(); }
		 */
	}

}
